#include "Sword.h"
#include "Actor.h"
#include "GameApp.h"

Sword::Sword(std::string id):
	BaseTrigger(id)
{
	activated = false;
	mTypes.push_front(OBJECT_SWORD);
	stun = 0.f;
	power = 0.f;
	target = OBJECT_ENEMY;
}

Sword::~Sword(void)
{
}

void Sword::activate(void)
{
	deactivate();
	activated = true;
}

void Sword::deactivate(void)
{
	activated = false;
	mHitList.clear();
}

void Sword::setPower(float pow, float stun, float knock)
{
	power = pow;
	this->stun = stun;
	knockBack = knock;
}

void Sword::collision(BaseObjectPtr obj, btVector3 &point)
{
	if(!activated) return;
	if(obj->hasType(target))
	{
		if (mpOwner)
		{
			ActorPtr actor = boost::static_pointer_cast<Actor>(obj);
			for(list<ActorPtr>::iterator it = mHitList.begin();it != mHitList.end();++it)
			{
				if(actor == (*it)) return;
			}
			btVector3 enemyPos = actor->getBody()->getWorldTransform().getOrigin();
			btTransform &trans = mpOwner->getBody()->getWorldTransform();
			if(mpOwner->getStates()[MOVE_LEFT] == ((trans.inverse() * enemyPos).x() > 0.f))
				return;

			
			actor->recieveDmg(mpOwner->getBody()->getWorldTransform().getOrigin(), power);
			actor->stun(stun);
			if(knockBack > 0.f) {
				btVector3 dir;
				if(mpOwner->getStates()[MOVE_LEFT])
					dir = btVector3(-1.f,0.f,0.f);
				else
					dir = btVector3(1.f,0.f,0.f);
				btVector3 knock = mpOwner->getBody()->getWorldTransform().getBasis() * dir.normalize() * knockBack;
				actor->knockBack(knock);
			}
			mpOwner->setHitEnemy(true);

			gpApp->mpGameLogic->createParticleSystem(Ogre::String("Blood1"), 1000, cvt(point));

			mHitList.push_back(actor);
		}
	}
}

void Sword::saveOffset(btTransform &trans)
{
	mOffset = btTransform(trans);
}

btTransform &Sword::getOffset(void)
{
	return mOffset;
}

void Sword::setOwner(ActorPtr owner)
{
	mpOwner = owner;
}

bool Sword::isActivated(void)
{
	return activated;
}

void Sword::setTarget(ObjectType type)
{
	target = type;
}